<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\HopeDrawServices;


class InnerHopeLuckController extends BaseController
{
    protected $wishLuckService;

    public function __construct()
    {
        $this->wishLuckService = new HopeDrawServices();

    }

    /**
     * 许愿池设置单次抽奖消耗数量
     */
    public function wishLuckSetNeedNum(Request $request)
    {
        $needNum = (int) $request->input('need_num', 0);
        if($needNum < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $ifLock = redis_lock('WISHLUCK:wishLuckSetNeedNum', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->editWishLuckConfig(['need_num'=>$needNum]);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池设置档位抽取次数
     */
    public function wishLuckSetDrawNum(Request $request)
    {
        $draw_num_1 = (int) $request->input('draw_num_1', 0);
        $draw_num_2 = (int) $request->input('draw_num_2', 0);
        $draw_img_1a = getUrlPath($request->input('draw_img_1a', ''));
        $draw_img_1b = getUrlPath($request->input('draw_img_1b', ''));
        $draw_img_2a = getUrlPath($request->input('draw_img_2a', ''));
        $draw_img_2b = getUrlPath($request->input('draw_img_2b', ''));
        if($draw_num_1<1 || $draw_num_2<1 || empty($draw_img_1a) || empty($draw_img_1b) || empty($draw_img_2a) || empty($draw_img_2b)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $ifLock = redis_lock('WISHPOOL:wishPoolSetDrawNum', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->editWishLuckConfig(['draw_num_1'=>$draw_num_1,'draw_num_2'=>$draw_num_2,'draw_img_1a'=>$draw_img_1a,'draw_img_1b'=>$draw_img_1b,'draw_img_2a'=>$draw_img_2a,'draw_img_2b'=>$draw_img_2b]);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池设置奖池开启/关闭
     */
    public function wishLuckSetPoolStatus(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolStatus = (int) $request->input('pool_status', 0);
        if(empty($poolId) || empty($poolStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $key = 'WISHPOOL:wishLuckPool';
        $ifLock = redis_lock($key, 60);
        if (!$ifLock){
            //Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '许愿池更新奖池中, 同一时间请勿重复请求');
        }

        $data = $this->wishLuckService->wishLuckSetPoolStatus($poolId, $poolStatus);
        redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池保存奖池配置
     */
    public function wishLuckSavePoolConfig(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $poolList = $request->input('pool_list');
        if(empty($poolId) || empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $poolList = json_decode($poolList, true);
        if(empty($poolList)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishLuckSavePoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckSavePoolConfig($poolId, $poolList);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池新增奖池
     */
    public function wishLuckAddPool(Request $request)
    {
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $poolType = (int) $request->input('pool_type', 0);
        $poolType = empty($poolType) ? 1 : $poolType;
        $ifDefault = (int) $request->input('if_default', 2);
        $ifDefault = empty($ifDefault) ? 2 : $ifDefault;
        if(empty($poolName)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(in_array($poolType, [1]) && (empty($minNum) || empty($maxNum))) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishLuckAddPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckAddPool($poolName, $minNum, $maxNum, $poolType, $ifDefault);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池编辑奖池
     */
    public function wishLuckEditPool(Request $request)
    {
        $poolId = $request->input('pool_id', 0);
        $poolName = $request->input('pool_name', '');
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $ifDefault = (int) $request->input('if_default', 0);
        if(empty($poolId) || empty($poolName)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishLuckEditPool', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckEditPool($poolId, $poolName, $minNum, $maxNum, $ifDefault);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池更新奖池缓存
     */
    public function wishLuckPoolReset(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        // $key = 'WISHPOOL:wishLuckPool';
        // $ifLock = redis_lock($key, 3600);
        // if (!$ifLock){
        //     Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '许愿池更新奖池中, 同一时间请勿重复请求');
        // }

        $data = $this->wishLuckService->wishLuckPoolReset($poolId);
        //redis_unlock($key);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池奖池配置剩余数量
     */
    public function wishLuckPoolConfigNum(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishLuckService->wishLuckPoolConfigNum($poolId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池新增白名单
     */
    public function wishLuckAddWhite(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $giftId = $request->input('gift_id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($userId) || empty($giftId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishLuckAddWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckAddWhite($userId, $giftId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池删除白名单
     */
    public function wishLuckDelWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishLuckDelWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckDelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 新增许愿池展示礼物
     */
    public function wishLuckAddGift(Request $request)
    {
        $giftId = (int) $request->input('gift_id', 0);
        $sort = (int) $request->input('sort', 0);
        if(empty($giftId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishLuckService->wishLuckAddGift($giftId, $sort);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池删除展示礼物
     */
    public function wishLuckDelGift(Request $request)
    {
        $id = (int) $request->input('id', 0);
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishLuckService->wishLuckDelGift($id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池新增个人池用户
     */
    public function wishLuckAddPoolUser(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $adminId = $request->input('admin_id',0);
        $poolId = (int) $request->input('pool_id', 0);
        $outDiamondNum = (int) $request->input('out_diamond_num', 0);
        if(empty($userId) || empty($poolId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $lockKey = 'WISHPOOL:poolUserMake';
        if (!redis_lock($lockKey, 300)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '个人池用户操作处理中, 同一时间请勿重复请求');
        }

        $data = $this->wishLuckService->wishLuckAddPoolUser($userId, $poolId, $outDiamondNum, $adminId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿池删除个人池用户
     */
    public function wishLuckDelPoolUser(Request $request)
    {
        $id = (int) $request->input('id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($id) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $lockKey = 'WISHWHEEL:poolUserMake';
        if (!redis_lock($lockKey, 100)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, '个人池用户操作处理中, 同一时间请勿重复请求');
        }

        $data = $this->wishLuckService->wishLuckDelPoolUser($id, $adminId);
        redis_unlock($lockKey);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    //查看个人奖池剩余数
    public function wishLuckPoolSelfConfigNum(Request $request){
        $poolId = (int) $request->input('pool_id', 0);
        $userId = (int) $request->input('user_id', 0);
        if(empty($poolId) || empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishLuckService->wishLuckPoolSelfConfigNum($poolId, $userId);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿池新增奖池白名单
     */
    public function wishLuckAddPoolWhite(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $giftId = $request->input('gift_id', 0);
        $adminId = $request->input('admin_id',0);
        if(empty($userId) || empty($giftId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishLuckAddPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckAddPoolWhite($userId, $giftId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池删除奖池白名单
     */
    public function wishLuckDelPoolWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishLuckDelPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckDelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池取消奖池白名单投放
     */
    public function wishLuckCancelPoolWhite(Request $request)
    {
        $whiteId = $request->input('white_id', 0);
        if(empty($whiteId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishLuckCancelPoolWhite', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckCancelWhite($whiteId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池奖池更新阀值
     */
    public function wishLuckSetUserPoolConfig(Request $request)
    {
        $poolId = (int) $request->input('pool_id', 0);
        $inProfitLoss = (int) $request->input('in_profit_loss', 0);
        $inProfitRate = (int) $request->input('in_profit_rate', 0);
        $outProfitRate = (int) $request->input('out_profit_rate', 0);
        if(empty($poolId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishLuckSetUserPoolConfig', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishLuckService->wishLuckSetUserPoolConfig($poolId, ['in_profit_loss'=>$inProfitLoss, 'in_profit_rate'=>$inProfitRate, 'out_profit_rate'=>$outProfitRate]);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 许愿池特殊玩法新增配置
     */
    public function wishLuckPoolConfigChildAdd(Request $request){
        $insertStr = $request->post('insertStr');
        $gift_type = $request->post('gift_type',0);
        $p_gift_id = $request->post('p_gift_id',0);
        $data = $this->wishLuckService->wishLuckPoolConfigChildAdd($insertStr, $gift_type, $p_gift_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池特殊玩法编辑
     */
    public function wishLuckPoolConfigChildEdit(Request $request){
        $insertStr = $request->post('insertStr');
        $sp_id = $request->post('sp_id',0);
        $p_gift_id = $request->post('p_gift_id',0);
    
        $data = $this->wishLuckService->wishLuckPoolConfigChildEdit($insertStr, $sp_id, $p_gift_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 许愿池特殊玩法删除
     */
    public function wishLuckPoolConfigChildDel(Request $request){
        $sp_id = $request->post('sp_id',0);
    
        $data = $this->wishLuckService->wishLuckPoolConfigChildDel($sp_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);

    }

    //许愿池新增投入展示礼物
    public function addWishLuckExGiftList(Request $request){
        $giftIds = $request->post('gift_ids','');//展示礼物ID
        $status = $request->post('is_open',0);//是否展示
        $data = $this->wishLuckService->addWishPoolsExGiftList($giftIds, $status);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    //新增奖池投入礼物
    public function addExGiftForPool(Request $request){
        $giftId = $request->post('gift_id','');//展示礼物ID
        $nums = $request->post('nums',0);//礼物数量
        $poolId = $request->post('pool_id',0);//奖池ID

        $data = $this->wishLuckService->addExGiftForPool($giftId, $nums, $poolId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    //查看投入记录
    public function getExGiftForPoolList(Request $request){
        $data = $this->wishLuckService->getExGiftForPoolList();

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }
}